<template>
  <view>
    <u-cell-group :border="false">
      <u-cell title="头像" border isLink @click="changeUserPhoto">
        <template #value>
          <image class="img" :src="useInfo?.avatar" shape="circle"></image>
        </template>
      </u-cell>
      <u-cell title="昵称" isLink @click="goPage('/pages/myPage/editPage/name')">
        <template #value>
          <text style="color: #bebebe">{{ useInfo?.nickName }}</text>
        </template>
      </u-cell>
      <u-cell title="UID">
        <template #value>
          <text style="color: #bebebe">{{ useInfo?.userId }}</text>
        </template>
      </u-cell>
      <u-cell title="实名认证" isLink @click="goPage('/pages/myPage/realNameAuth')">
        <template #value>
          <text style="color: #bebebe">{{ authInfo[useInfo?.authFlag] }}</text>
        </template>
      </u-cell>
      <u-cell title="我的收货地址" isLink @click="goPage('/pages/myPage/myShippingAddress')"> </u-cell>
    </u-cell-group>
  </view>
</template>

<script setup lang="ts">
import { BASE_UPLOADURL } from "@/utils/request";
import { ref } from "vue";
import { goPage } from "@/utils/commonFn";

// @ts-ignore
import { onShow } from "@dcloudio/uni-app";
import { selectUserInfoByUserId } from "@/api/my";
const useInfo = ref();
const authInfo: any = ref({
  "0": "未认证",
  "1": "已认证",
  "2": "认证中",
});

onShow(async () => {
  const res = await selectUserInfoByUserId({ userId: uni.getStorageSync("userId") });
  useInfo.value = res;
});

const changeUserPhoto = () => {
  uni.chooseImage({
    count: 1, //默认9
    sizeType: ["original", "compressed"], //可以指定是原图还是压缩图，默认二者都有
    sourceType: ["album", "camera"], //从相册选择或者打开相机
    success: function (res: any) {
      if (res.tempFiles[0].size > 2 * 1024 * 1024) {
        uni.showToast({
          title: "图片大小不得超过2MB",
          icon: "none",
        });
      } else {
        uni.showLoading({
          title: "加载中",
        });
        let tempFilePaths = res.tempFilePaths;
        for (let i = 0, len = tempFilePaths.length; i < len; i++) {
          uni.uploadFile({
            url: BASE_UPLOADURL, //仅为示例，非真实的接口地址
            filePath: tempFilePaths[i],
            name: "file",
            header: {
              Authorization: uni.getStorageSync("token"),
            },
            success: async (uploadFileRes) => {
              console.log(uploadFileRes);
              // await userEdit({ avatar: JSON.parse(uploadFileRes.data).data });
              useInfo.value.avatar = JSON.parse(uploadFileRes.data).data;
              uni.showToast({
                title: "修改成功",
              });
            },
          });
        }
      }
    },
  });
};
</script>

<style lang="less" scoped>
.img {
  width: 76rpx;
  height: 76rpx;
  background: #ebeff8;
  border-radius: 50%;
}
/deep/.u-cell-group {
  margin-top: 20rpx;
  overflow: hidden;
  background-color: #fff;
}

/deep/ .u-cell-group__wrapper {
  padding: 0rpx 32rpx;
}

/deep/ .u-cell__body {
  padding: 36rpx 0rpx;
}

/deep/ .u-cell__title-text {
  font-size: 30rpx;
  font-family: PingFangSC-Regular, PingFang SC;
  color: #333;
}

/deep/ .u-cell__value {
  font-size: 28rpx;
  font-weight: 400;
  color: #333;
}

:deep(.u-line) {
  border-color: #ffffff !important;
}
</style>
